package com.action.ui.activity;

import android.animation.Animator;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.action.childbase.ChildBaseActivity;
import com.action.entity.RedPacketCommentEvent;
import com.action.entity.XyBUserRecvRed;
import com.action.entity.event.RedPacketChangeEvent;
import com.action.service.pressenter.RedpakcetPressenter;
import com.action.ui.adapter.ComFragmentAdapter;
import com.action.ui.adapter.ShowImageAdapter;
import com.action.ui.fragment.RedDetailCommentListFrag;
import com.action.ui.fragment.RedDetailUserListFrag;
import com.action.ui.view.ColorFlipPagerTitleView;
import com.action.ui.view.JudgeNestedScrollView;
import com.action.utils.GridDividerItemDecoration;
import com.action.utils.PhoneUtil;
import com.action.utils.SingleClick;
import com.action.utils.SoftKeyBoardListener;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.app_core.base.ActionPacketView;
import com.base.app_core.model.Paginated;
import com.base.app_core.service.user.model.HomeRxBean;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.xinglai.yun.R;
import com.hwangjr.rxbus.RxBus;
import com.luseen.autolinklibrary.AutoLinkMode;
import com.luseen.autolinklibrary.AutoLinkOnClickListener;
import com.luseen.autolinklibrary.AutoLinkTextView;
import com.nineoldandroids.animation.ObjectAnimator;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.map.lib.util.StringUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;

//红包详情
@Route(path = "/redpacket/detail")
public class RedPacketDetail extends ChildBaseActivity<RedpakcetPressenter> implements
        SoftKeyBoardListener.OnSoftKeyBoardChangeListener
        , ActionPacketView.RedPacketAction, ShowImageAdapter.onDefaultImageAddLisener {
    String sn;
    @BindView(R.id.redpacket_sender_img)
    ImageView redpacketSenderImg;
    @BindView(R.id.sender_nick_name)
    TextView senderNickName;
    @BindView(R.id.red_recv_price)
    TextView redRecvPrice;

    @BindView(R.id.nestedScroll)
    JudgeNestedScrollView nestedScrollView;

    @BindView(R.id.dan)
    TextView dan;

    @BindView(R.id.title)
    RelativeLayout title;

    @BindView(R.id.red_gufen_price)
    TextView redGufenPrice;

    @BindView(R.id.red_content)
    AutoLinkTextView redContent;

    @BindView(R.id.signal_img)
    ViewStub signalImg;

    @BindView(R.id.more_imgs)
    ViewStub moreImgs;

    @BindView(R.id.magic_indicator_title)
    MagicIndicator magicIndicatorTitle;

    @BindView(R.id.close_category_list)
    ImageView closeIv;

    @BindView(R.id.text_close)
    TextView closeTv;

    @BindView(R.id.tv_range)
    TextView tvRange;

    @BindView(R.id.view_pager)
    ViewPager viewPager;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.comment_ll)
    LinearLayout commentll;

    RecyclerView imgContainer;

    @BindView(R.id.comment_content)
    EditText commentContent;

    @BindView(R.id.mline)
    View line;

    @BindView(R.id.post_comment)
    TextView postComment;

    @BindView(R.id.ad_detail)
    LinearLayout mAdDetail;

    public void gotoAdDetail() {
        if (redpacket != null && !StringUtil.isEmpty(redpacket.getUrl())) {//跳转浏览器
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(redpacket.getUrl());
            intent.setData(content_url);
            startActivity(intent);
        }
    }

    private RedDetailUserListFrag redDetailUserListFrag = null;

    private RedDetailCommentListFrag redDetailCommentListFrag = null;

    private ShowImageAdapter addImageAdapter;
    private SoftKeyBoardListener softKeyBoardListener;
    private XyBUserRecvRed redpacket;
    private int type;
    private boolean fromOpen;

    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;

    private List<String> mDataList = new ArrayList<>();
    private CommonNavigatorAdapter commonNavigatorAdapter;
    private int toolBarPositionY;
    private CommonNavigatorAdapter commonNavigatorTitleAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_red_packet_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        sn = getIntent().getStringExtra("sn");
        type = getIntent().getIntExtra("type", -1);
        fromOpen = getIntent().getBooleanExtra("fromOpen", true);
        if (StringUtils.isEmpty(sn)) {
            finish();
            return;
        }
        if (fromOpen)
            setSwipeBackEnable(false);

        initTypeView();
//        initCursorLineWith();
        TextPaint paint = postComment.getPaint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.UNDERLINE_TEXT_FLAG);
        nestedScrollView.setClickView(mAdDetail);
        setToolBar("红包信息");

        redDetailUserListFrag = new RedDetailUserListFrag();
        redDetailUserListFrag.setRedpacketSn(sn);

        redDetailCommentListFrag = new RedDetailCommentListFrag();
        redDetailCommentListFrag.setRedpacketSn(sn);

        fragmentList.add(redDetailUserListFrag);
        fragmentList.add(redDetailCommentListFrag);

//        tabTextList.add(tabDetailText);
//        tabTextList.add(tabInstallText);

        nestedScrollView.setNeedScroll(true);
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int currentItem = viewPager.getCurrentItem();
                if (getFragmentListIsEmpty(currentItem))
                    return;
                int[] location = new int[2];
                magicIndicator.getLocationOnScreen(location);
                int yPosition = location[1];
                if (yPosition < toolBarPositionY) {
                    magicIndicatorTitle.setVisibility(View.VISIBLE);
                    line.setVisibility(View.VISIBLE);
                    nestedScrollView.setNeedScroll(false);
                } else {
                    magicIndicatorTitle.setVisibility(View.GONE);
                    line.setVisibility(View.GONE);
                    nestedScrollView.setNeedScroll(true);
                }
            }
        });

        viewPager.setAdapter(new ComFragmentAdapter(getSupportFragmentManager(), fragmentList));

        viewPager.setOffscreenPageLimit(2);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (nowIndex == 0) {
                    redDetailUserListFrag.setPageNo(1);
                    redDetailUserListFrag.fetchUserList();
                } else {
                    redDetailCommentListFrag.setPageNo(1);
                    redDetailCommentListFrag.fetchCommentList();
                }
                fetchRedpacketDetail();
                if (refreshlayout != null) {
                    refreshlayout.finishRefresh(1500);
                }
            }
        });
        softKeyBoardListener = new SoftKeyBoardListener(this);
        softKeyBoardListener.setListener(this, this);

        imgContainer = (RecyclerView) moreImgs.inflate();
        addImageAdapter = new ShowImageAdapter();
        addImageAdapter.setOnDefaultImageAddLisener(this);
        addImageAdapter.setShowDeleteButton(false);
        GridLayoutManager layout = new GridLayoutManager(this, 3);
        imgContainer.setLayoutManager(layout);
        imgContainer.addItemDecoration(new GridDividerItemDecoration(XYBAUtils.dip2px(8), getResources().getColor(R.color.white)));
        imgContainer.setAdapter(addImageAdapter);

        mDataList.add("已领取");
        mDataList.add("评价");
        initMagicIndicator();
        initMagicIndicatorTitle();

        title.post(new Runnable() {
            @Override
            public void run() {
                dealWithViewPager();
            }
        });

        if (PhoneUtil.isHUAWEI() && PhoneUtil.checkDeviceHasNavigationBar(this.getApplicationContext())) {
            ContentResolver contentResolver = getContentResolver();
            contentResolver.registerContentObserver(Settings.System.getUriFor
                    ("navigationbar_is_min"), true, mNavigationStatusObserver);
        }
        showProgress();
        redContent.addAutoLinkMode(
                AutoLinkMode.MODE_HASHTAG,
                AutoLinkMode.MODE_PHONE,
                AutoLinkMode.MODE_URL);
        redContent.setUrlModeColor(ContextCompat.getColor(this, R.color.green));
        redContent.enableUnderLine();
        redContent.setAutoLinkOnClickListener(new AutoLinkOnClickListener() {
            @Override
            public void onAutoLinkTextClick(AutoLinkMode autoLinkMode, String matchedText) {
                switch (autoLinkMode) {
                    case MODE_URL:
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(matchedText);
                        intent.setData(content_url);
                        intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
                        RedPacketDetail.this.startActivity(intent);
                        break;
                    case MODE_EMAIL:
                        break;
                    case MODE_PHONE:
                        break;
                }
            }
        });
    }

    private boolean getFragmentListIsEmpty(int currentItem) {
        RedDetailUserListFrag.RedUserAdapter redUserAdapter = redDetailUserListFrag.getRedUserAdapter();
        if (redUserAdapter == null) {
            return false;
        }
        if (currentItem == 0 && XYBAUtils.isEmpty(redUserAdapter.getList())) {
            return true;
        } else {
            RedDetailCommentListFrag.RedUserCommentAdapter redUserCommentAdapter = redDetailCommentListFrag.getRedUserCommentAdapter();
            if (redUserCommentAdapter == null) {
                return false;
            }
            if (currentItem == 1 && XYBAUtils.isEmpty(redUserCommentAdapter.getList())) {
                return true;
            }
        }
        return false;
    }

    private void initMagicIndicatorTitle() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(true);
        commonNavigatorTitleAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(RedPacketDetail.this, R.color.text_black));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(RedPacketDetail.this, R.color.red));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index, false);
                        nowIndex = index;
                        if (getFragmentListIsEmpty(index)) {
                            nestedScrollView.setNeedScroll(true);
                        }
                        initCommentLl();
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, AppConstants.LINE_CURSOR_WIDTH));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(RedPacketDetail.this, R.color.red));
                return indicator;
            }
        };
        commonNavigator.setAdapter(commonNavigatorTitleAdapter);
        magicIndicatorTitle.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicatorTitle, viewPager);
    }

    private void initCommentLl() {
        if (nowIndex == 1) {
            YoYo.with(Techniques.FadeIn).duration(200).onEnd(new YoYo.AnimatorCallback() {
                @Override
                public void call(Animator animator) {
                    commentll.setVisibility(View.VISIBLE);
                }
            }).playOn(commentll);
        } else {
            commentll.setVisibility(View.GONE);
        }
    }

    private ContentObserver mNavigationStatusObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            dealWithHuaWei();
        }
    };

    /**
     * 处理华为虚拟键显示隐藏问题导致屏幕高度变化，ViewPager的高度也需要重新测量
     */
    private void dealWithHuaWei() {
        refreshLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                dealWithViewPager();
                refreshLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    private void dealWithViewPager() {
        toolBarPositionY = title.getHeight();
        ViewGroup.LayoutParams params = viewPager.getLayoutParams();
        params.height = XYBAUtils.getWindowHeight() - toolBarPositionY - magicIndicator.getHeight() + 1;
        viewPager.setLayoutParams(params);
    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(true);
        commonNavigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(RedPacketDetail.this, R.color.black));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(RedPacketDetail.this, R.color.red));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        nowIndex = index;
                        viewPager.setCurrentItem(index, false);
                        initCommentLl();
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, AppConstants.LINE_CURSOR_WIDTH));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(RedPacketDetail.this, R.color.red));
                return indicator;
            }
        };
        commonNavigator.setAdapter(commonNavigatorAdapter);
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


    /**
     * 初始化红包类型的view
     */
    private void initTypeView() {
        try {
            if (fromOpen) {
                closeTv.setText(String.valueOf(AppConstants.DEFAULT_AD_TIME));
                closeTv.setVisibility(View.VISIBLE);
                closeIv.setVisibility(View.GONE);
            } else {
                closeTv.setVisibility(View.GONE);
                closeIv.setVisibility(View.VISIBLE);
            }

            requestPageClose();
            redContent.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {
        super.initData();
        fetchRedpacketDetail();
    }

    private void fetchRedpacketDetail() {
        if (!StringUtils.isEmpty(sn) && null != mPresenter) {
            mPresenter.fetchRedpacketDetail(sn);
        }
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
        super.notifyData(object, tClass, paginated);
        if (object instanceof XyBUserRecvRed) {//红包详情
            redpacket = (XyBUserRecvRed) object;
            XYBAUtils.displayCircleImage(this, redpacket.getWxIcon(), redpacketSenderImg);
            senderNickName.setText(redpacket.getWxNickname() + "的红包");
            String userPrice = redpacket.getUserPrice();
            if (StringUtils.isEmpty(userPrice)) {
                redRecvPrice.setVisibility(View.GONE);
                dan.setVisibility(View.GONE);
            } else {
                redRecvPrice.setText(new BigDecimal(userPrice).setScale(4, RoundingMode.DOWN).toPlainString());
                redRecvPrice.setVisibility(View.VISIBLE);
                dan.setVisibility(View.VISIBLE);
            }
            String stockCount = redpacket.getStockCount();
            if (StringUtils.isEmpty(stockCount)) {
                redGufenPrice.setVisibility(View.GONE);
            } else {
                redGufenPrice.setVisibility(View.VISIBLE);
                redGufenPrice.setText("已存入:" + stockCount + "红包股");
            }
            String target = redpacket.getContent().replace("http://", " http://")
                    .replace("https://", " https://")
                    .replace("rtsp://", " rtsp://")
                    .replace("mailto:", " mailto:");
            redContent.setAutoLinkText(target);
            redDetailUserListFrag.setRepacket(redpacket);
            if (redpacket.getRange() > 0) {
                tvRange.setText(redpacket.getRange() / 1000 + "km");
            } else if (redpacket.getRange() == 0) {
                tvRange.setText("全区");
            } else if (redpacket.getRange() == -1) {
                tvRange.setText("全市");
            } else if (redpacket.getRange() == -2) {
                tvRange.setText("全省");
            } else if (redpacket.getRange() == -3) {
                tvRange.setText("全国");
            }
            mDataList.set(0, redpacket.getRecvCount() == 0 ? "已领取" : "已领取(" + redpacket.getRecvCount() + ")");
            mDataList.set(1, setCommontText(false));
            commonNavigatorAdapter.notifyDataSetChanged();
            commonNavigatorTitleAdapter.notifyDataSetChanged();
//            tabDetailText.setText();
//            tabInstallText.setText(setCommontText(false));
            String imgs = redpacket.getImgs();
            if (!StringUtil.isEmpty(imgs)) {
                if (imgs.contains(",")) {
                    List<String> split = Arrays.asList(imgs.split(","));
                    if (!XYBAUtils.isEmpty(split)) {
                        addImageAdapter.notifyDataSetChanged(split);
                    }
                } else {
                    ImageView singImg = signalImg.inflate().findViewById(R.id.img_container);
                    ViewGroup.LayoutParams layoutParams = singImg.getLayoutParams();
                    layoutParams.width = XYBAUtils.getScreenWidth() - XYBAUtils.dip2px(16) * 2;
                    layoutParams.height = XYBAUtils.getScreenWidth() - XYBAUtils.dip2px(16) * 2;
                    singImg.setLayoutParams(layoutParams);
                    XYBAUtils.displayImage(imgs, singImg);
                    singImg.setVisibility(View.VISIBLE);
                    singImg.setClickable(true);
                    singImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            gotoAdDetail();
                        }
                    });
                }
                imgContainer.setVisibility(View.VISIBLE);
            } else {
                imgContainer.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void notifyError(String errorCode, String content) {
        super.notifyError(errorCode, content);
    }

    @NonNull
    private String setCommontText(boolean man) {
        int commentCount = redpacket.getCommentCount();
        if (man) {
            commentCount++;
            redpacket.setCommentCount(commentCount);
            return "评价(" + commentCount + ")";
        } else {
            return commentCount == 0 ? "评价" : "评价(" + commentCount + ")";
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        System.out.println("=====");
        long timeOutCount = mPresenter.getmTick();
        if (fromOpen && timeOutCount > 0) {
            showToast("请等待" + timeOutCount + "秒再退出");
            return;
        }
        super.onBackPressed();
    }

    @OnClick(R.id.close_category_list)
    public void onViewClicked() {
    }

    protected List<Fragment> fragmentList = new ArrayList<>();
    protected float fromX;
    protected int nowIndex = 0;


    @OnClick(R.id.post_comment)
    public void onClickPostComment() {
        if (SingleClick.isSingle()) {
            showProgress();
            String comment = commentContent.getText().toString();
            if (StringUtil.isEmpty(comment)) {
                showToast("请输入评论内容");
                return;
            }
            redDetailCommentListFrag.postComment(comment);
        }
    }


    @Override
    public void keyBoardShow(int height) {
        ObjectAnimator translationY = ObjectAnimator.ofFloat(commentll, "translationY",
                0, shouTranY(height));
        translationY.setDuration(AppConstants.ANI_DIALOG_FLOATING_TOP / 2);
        translationY.start();
    }

    @Override
    public void keyBoardHide(int height) {
        ObjectAnimator translationY = ObjectAnimator.ofFloat(commentll, "translationY",
                shouTranY(height), 0);
        translationY.setDuration(AppConstants.ANI_DIALOG_FLOATING_TOP / 2);
        translationY.start();
    }

    private int shouTranY(int height) {
        return -height;
    }

    public void setCommonSuccessAfter() {
        commentContent.setText("");
        if (redpacket != null) {
            RxBus.get().post(new RedPacketCommentEvent(redpacket.getId(),
                    redpacket.getCommentCount() + 1));
        }
//        tabInstallText.setText(setCommontText(true));
        KeyboardUtils.hideSoftInput(commentContent);
    }

    @Override
    public void updateCloseView(int tick) {
        closeTv.setText(String.valueOf(tick));
        if (tick == 0) {
            closeTv.setVisibility(View.GONE);
            closeIv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (fromOpen && redpacket != null) {
            RedPacketChangeEvent event = new RedPacketChangeEvent();
            event.setRedpacket(redpacket);
            event.setStatus(RedPacketChangeEvent.PACKET_ROBED);
            RxBus.get().post(new HomeRxBean(HomeRxBean.SEND_REDPACKET, event));
        }
    }

    @Override
    public void onClickAction(boolean b, String item) {
        if (b) {
            addImageAdapter.previewImage(this, item);
        } else {
            PhotoPicker.builder()
                    .setPhotoCount(1)
                    .setShowCamera(true)
                    .setShowGif(false)
                    .setPreviewEnabled(true)
                    .setShowCamera(true)
                    .start(this, PhotoPicker.REQUEST_CODE);
        }
    }

    public boolean isOnce = true;

    public void requestPageClose() {
        if (fromOpen && isOnce) {
            isOnce = false;
            mPresenter.notifyCloseTextChange();
        }
    }

}
